// pages/user/company/company.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        companyInfo: {},
        companyStock: [],
        companyProperty: {},
        companyData: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getCompanyInfo()
            }
        }).catch(function(e) {})
    },

    getCompanyInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let filialeId = this.data.userInfo.filiale_id
        let companyInfo = this.data.companyInfo
        let data = {
            filiale_id: filialeId
        }
        common.post('Holder/get_store', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    companyInfo: companyInfo
                })
            } else {
                that.setData({
                    companyInfo: res
                })
                that.getCompanyStock()
                that.getCompanyProperty()
                that.getCompanyData()
            }
        }).catch(function(e) {})
    },

    getCompanyStock: function() {
        const that = this
        let filialeId = this.data.companyInfo.filiale_id
        let companyStock = this.data.companyStock
        let data = {
            filiale_id: filialeId
        }
        common.post('Holder/get_holder', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    companyStock: companyStock
                })
            } else {
                that.setData({
                    companyStock: res
                })
            }
        }).catch(function(e) {})
    },

    getCompanyProperty: function() {
        const that = this
        let filialeId = this.data.companyInfo.filiale_id
        let companyProperty = this.data.companyProperty
        let data = {
            filiale_id: filialeId
        }
        common.post('Holder/get_holder_property', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    companyProperty: companyProperty
                })
            } else {
                that.setData({
                    companyProperty: res
                })
            }
        }).catch(function(e) {})
    },

    getCompanyData: function() {
        const that = this
        let filialeId = this.data.companyInfo.filiale_id
        let companyData = this.data.companyData
        let data = {
            filiale_id: filialeId
        }
        common.post('Holder/get_holder_user', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    companyData: companyData
                })
            } else {
                that.setData({
                    companyData: res
                })
            }
        }).catch(function(e) {})
    },

    goPropertyDetail: function(e) {
        wx.navigateTo({
            url: '/pages/user/company/companyProperty/companyProperty?company=' + e.currentTarget.dataset.company
        })
    },

    goCompanyData: function(e) {
        wx.navigateTo({
            url: '/pages/user/company/companyData/companyData?type=' + e.currentTarget.dataset.type + '&company=' + e.currentTarget.dataset.company
        })
    }
})